package com.jajahome.feature.user.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jajahome.R;
import com.jajahome.base.BaseRecyclerAdapter;
import com.jajahome.base.BaseRecyclerViewHolder;
import com.jajahome.constant.Constant;
import com.jajahome.feature.H5Act;
import com.jajahome.feature.home.MainAty;
import com.jajahome.feature.house.SelectCityAct;
import com.jajahome.feature.user.activity.CrowAct;
import com.jajahome.feature.user.activity.FriendListAct;
import com.jajahome.feature.user.activity.IntegrateAct;
import com.jajahome.feature.user.activity.LoginAct;
import com.jajahome.feature.user.activity.MyAddressAct;
import com.jajahome.feature.user.activity.MyCollectAct;
import com.jajahome.feature.user.activity.OrderListAct;
import com.jajahome.feature.user.activity.RebateInfoAct;
import com.jajahome.feature.user.activity.SettingAct;
import com.jajahome.feature.user.activity.SignAct;
import com.jajahome.feature.user.activity.UserLogAct;
import com.jajahome.feature.user.activity.ValueAct;
import com.jajahome.model.LoginModle;
import com.jajahome.model.UserActionModel;
import com.jajahome.util.LocationSvc;
import com.jajahome.util.LoginUtil;
import com.jajahome.util.StringUtil;
import com.jajahome.widget.SharedPreferencesUtils;

import java.util.List;

/**
 * Created by tuxuesong on 2018/4/26.
 */
public class UserItemAdapter extends BaseRecyclerAdapter<UserActionModel> {

    private ActionListener listener;

    public void setListener(ActionListener listener) {
        this.listener = listener;
    }


    @Override
    public void showData(BaseRecyclerViewHolder viewHolder, int i, List<UserActionModel> mItemDataList) {

        final ViewHolder holder = (ViewHolder) viewHolder;
        holder.actionImg.setImageResource(mItemDataList.get(i).getResource());
        holder.actionDesc.setText(mItemDataList.get(i).getAction());
        holder.actionText.setText(mItemDataList.get(i).getText());
        holder.mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                String desc = (String) holder.actionDesc.getText().toString();
                if (desc.equals("全部订单")) {
                    intent.setClass(mContext, OrderListAct.class);
                } else if (desc.equals("收藏")) {
                    intent.setClass(mContext, MyCollectAct.class);
                } else if (desc.equals("优惠券")) {
                    intent.setClass(mContext, ValueAct.class);
                } else if (desc.equals("会员中心")) {
                    return;
                } else if (desc.equals("分享邀请")) {
                    if (listener != null) {
                        listener.setAction("share");
                        return;
                    }
                } else if (desc.equals("好友")) {
                    intent.setClass(mContext, FriendListAct.class);
                } else if (desc.equals("奖励")) {
                    intent.setClass(mContext, RebateInfoAct.class);
                } else if (desc.equals("金币")) {
                    intent.setClass(mContext, IntegrateAct.class);
                } else if (desc.equals("足迹")) {
                    intent.setClass(mContext, UserLogAct.class);
                } else if (desc.equals("众筹")) {
                         intent.setClass(mContext, CrowAct.class);
                } else if (desc.equals("找我家")) {
                    LoginModle loginModle = LoginUtil.getInfo(mContext);
                    if (loginModle == null) {
                        intent.setClass(mContext, LoginAct.class);
                    } else {
                        intent.setClass(mContext, SelectCityAct.class);
                        SharedPreferencesUtils sharedPreferencesUtils = new SharedPreferencesUtils(mContext);
                        if (!StringUtil.isEmpty(sharedPreferencesUtils.getUserCity())){
                            intent.putExtra(Constant.LOCATION, sharedPreferencesUtils.getUserCity());
                        }
                    }
                } else if (desc.equals("签到")) {
                    intent.setClass(mContext, SignAct.class);
                } else if (desc.equals("地址")) {
                    intent.setClass(mContext, MyAddressAct.class);
                } else if (desc.equals("客服")) {
                    String cityName = LocationSvc.sharedPreferencesUtils.getUserCity();
                    String about_us = Constant.ABOUT_US + "&city=" + cityName;
                    H5Act.gotoH5(mContext, about_us, "联系我们");
                    return;
                } else if (desc.equals("帮助")) {
                    return;
                } else if (desc.equals("设置")) {
                    intent.setClass(mContext, SettingAct.class);
                }
                if (! MainAty.isFastClick()) {
                    mContext.startActivity(intent);
                }
            }
        });

        switch (i) {
            case 3:
                holder.actionImg.setVisibility(View.INVISIBLE);
                holder.actionDesc.setVisibility(View.INVISIBLE);
                holder.actionText.setVisibility(View.INVISIBLE);
                break;
        }


    }

    @Override
    public int getListLayoutId() {
        return R.layout.act_user_item;
    }

    @Override
    public BaseRecyclerViewHolder createViewHolder(View view) {
        return new ViewHolder(view);
    }


    class ViewHolder extends BaseRecyclerViewHolder {

        private ImageView actionImg;
        private TextView actionDesc;
        private TextView actionText;
        private LinearLayout mLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            actionImg = (ImageView) findView(R.id.action_img);
            actionDesc = (TextView) findView(R.id.action_desc);
            actionText = (TextView) findView(R.id.action_num);
            mLayout = (LinearLayout) findView(R.id.parent);
        }
    }


    public interface ActionListener {
        void setAction(String action);
    }


}
